package level.editor.tool;

import java.util.HashMap;

import javax.swing.JTabbedPane;

import level.editor.command.Command;
import level.editor.mediator.Module;
import level.editor.tool.collision.ToolCollision;
import level.editor.tool.layer.ToolLayer;
import level.editor.tool.object.ToolObject;
import level.editor.tool.tile.ToolTile;
import level.editor.tool.trigger.ToolTrigger;
import level.editor.window.View;
import level.editor.window.Window;

public class ToolView extends JTabbedPane implements View
{
	private Module module;
	private HashMap<String, Tool> tools;
	private static final int CAPACITY = 10;
	private static final int LOADFACTOR = 1;

	public ToolView()
	{
		super();
		tools = new HashMap<String, Tool>(CAPACITY, LOADFACTOR);
	}

	public void init(Window window)
	{
		initToolTile();
		initToolCollision();
		initToolObject();
		initToolTrigger();
		initToolLayer();
//		initToolOption();
		window.addWest(this);
	}

	private void initToolTile()
	{
		ToolTile tile = new ToolTile(this);
		tools.put("ToolTile", tile);
		addTab("Tiles", tile);
	}

	private void initToolCollision()
	{
		ToolCollision collision = new ToolCollision(this);
		tools.put("ToolCollision", collision);
		addTab("Collisions", collision);
	}

	private void initToolObject()
	{
		ToolObject object = new ToolObject(this);
		tools.put("ToolObject", object);
		addTab("Objects", object);
	}

	private void initToolTrigger()
	{
		ToolTrigger trigger = new ToolTrigger(this);
		tools.put("ToolTrigger", trigger);
		addTab("Triggers", trigger);
	}

	private void initToolLayer()
	{
		ToolLayer layer = new ToolLayer(this);
		tools.put("ToolLayer", layer);
		addTab("Layers", layer);
	}

//	private void initToolOption()
//	{
//		ToolOption option = new ToolOption(this);
//		tools.put("ToolOption", option);
//		addTab("Options", option);
//	}

	public void pushDownCommand(Command command) // Requires generalization
	{
//		if (command instanceof UpdateOptionLevelCommand)
//		{
//			UpdateOptionLevelCommand c = (UpdateOptionLevelCommand) command;
//			ToolOption option = (ToolOption) tools.get("OPTION");
//			option.updateCommand(c.getOption(), c.isTrue(), c.isToggle());
//		}

		if (command instanceof ToolCommand)
		{
			ToolCommand c = (ToolCommand) command;
			tools.get(c.getToolDestination()).pushDownCommand(c);
		}
	}

	public void pushUpCommand(Command command)
	{
		module.pushUpCommand(command);
	}

	@Override
	public void registerModule(Module module)
	{
		this.module = module;
	}

}
